/*
 * $Id: djquery.js 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
 * Copyright (c) 2009 Prabhu Gurumurthy  <pgurumur@gmail.com>
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * $Revision: 7557b5d4d82b $ 
 * $Author: pgurumur $ 
 */

$(function() {

   var $forms = $("form");
   $forms.focusin(function() {
      $forms.removeClass("active-form");
      $(this).addClass("active-form");
   });

   $("#dialog:ui-dialog").dialog("destroy");
   tips = $(".validateTips");

   function help(message) {
      tips.text(message).addClass("ui-state-highlight");
      setTimeout(function() {
         tips.removeClass("ui-state-highlight", 1500);
      }, 6000);
      tips.text("Length is required!");
   }

   function checklength(obj, num, min, max) {
      retval = true;
      if (obj.val().length > max || obj.val().length < min) {
         obj.addClass("ui-state-error");
         help("Length of " + num + " must be between " + min + " and " +
            max + ".");
         retval = false;
      }
      return retval;
   }

   function checkInteger(obj, name, min, max) {
      retval = true;
      if (isNaN(parseInt(obj.val()))) {
         help("Invalid length " + obj.val());
         retval = false;
      }
      else {
         if ((obj.val() >= min) && (obj.val() <= max)) {
            retval = true;
         }
         else {
            help("Invalid length " + obj.val() + " value should be between " +
               min + " and " + max);
            retval = false;
         }                
      }
      
      return retval;
   }

   function GenerateRandomString(Length) {
      var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
      var randstring = '';

      for (var ix = 0; ix < Length; ix++) {
         var num = Math.floor(Math.random() * chars.length);
         randstring += chars.substring(num, (num + 1));
      }

      return randstring;
   }

   function clearform() {
      $("#length").val("6");
      $("#randstr").val("");
      tips.text("Length is required!");
      return false;
   }

   
   $("button").button();
   $("#clrstr").click(function() {
      clearform();
      return false;
   });

   $("#generate").click(function() {
      var bValid = true;
      bValid = bValid && checkInteger($("#length"), "length", 6, 24);
      if (bValid) {
         var randstring = GenerateRandomString($("#length").val());
         $("#randstr").val(randstring);
      }

      return false;
   });

   $( "#accordion" ).accordion({
      autoHeight: false,
      navigation: true
      });

   $( "#tabs" ).tabs();
   function CreateMask() {
      var retval = [];
      for (ix = 0; ix < 32; ix++) {
         retval.push(ix.toString());
      }

      return retval;
   }
   
   var maskopts = [ "WildCard", "Netmask" ];

   $("#masklen").autocomplete({ source: CreateMask() });
   $("#maskopt").autocomplete({ source: maskopts });
   $("#djdialog").dialog({
      modal: true,
      buttons: {
         Ok: function() { $( this ).dialog( "close" ); }
      }
   });

   function MsgBox(Title, Message) {
      $("#djmesg").val(Message);
      $("#djdialog").dialog("option", "title", Title);
      $("#djdialog").dialog("open");
   }
});
